package org.javaboy.util;

import com.sun.javafx.binding.StringFormatter;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class StringUtil {
    /**
     * 字符串转换unicode
     */
    public static String string2Unicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }

    /**
     * 字符串转换成为16进制(无需Unicode编码)
     * @param str
     * @return
     */
    public static String string2Hex(String str) {
        char[] chars = "0123456789abcdef".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * 16进制直接转换成为字符串(无需Unicode解码)
     * @param hexStr
     * @return
     */
    public static String hex2String(String hexStr) {
        String str = "0123456789abcdef";
        char[] hexs = hexStr.toCharArray();
        byte[] bytes = new byte[hexStr.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes);
    }

    /**
     * unicode 转字符串
     */
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }
        return string.toString();
    }

    public static String urlEncode(String str){
        try {
            return URLEncoder.encode(str,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String urlDecode(String encodedString){
        try {
            return URLDecoder.decode(encodedString,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return encodedString;
    }

    public static String encrypt(String source){
        return string2Hex(urlEncode(source));
    }

    public static String decrypt(String encryptedStr){
        return urlDecode(hex2String(encryptedStr));
    }

    public static String generateAutoLoginToken(String username,String password){
        return String.format("%s-%s",base64Encoder(username),base64Encoder(password));
    }

    /**
     * BASE64加密
     * @param src
     * @return
     */
    public static String base64Encoder(String src) {
        String encoding="UTF-8";
        String ciphertext = null;

        try {
            BASE64Encoder b64encoder = new BASE64Encoder();
            ciphertext = b64encoder.encode(src.getBytes(encoding));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return ciphertext;
    }

    /**
     * BASE64解密
     * @param dest
     * @return
     **/
    public static String base64Decoder(String dest) {
        String encoding="UTF-8";
        byte[] b = null;

        try {
            BASE64Decoder b64decoder = new BASE64Decoder();
            b = b64decoder.decodeBuffer(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String plaintext = null;
        try {
            plaintext = new String(b, encoding);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return plaintext;
    }

    public static void main(String[] args) {
        String name="监管员";
        String pwd="jiucang999";

        String encodeName,encodePwd;

        encodeName=encrypt(name);
        encodePwd=encrypt(pwd);

        name=decrypt(encodeName);
        pwd=decrypt(encodePwd);

        encodeName=string2Hex(name);
        encodePwd=string2Hex(pwd);

        name=hex2String(encodeName);
        pwd=hex2String(encodePwd);

        encodeName=string2Unicode(name);
        encodePwd=string2Unicode(pwd);

        name=unicode2String(encodeName);
        pwd=unicode2String(encodePwd);

        encodeName=urlEncode(name);
        encodePwd=urlEncode(pwd);

        name=urlDecode(encodeName);
        pwd=urlDecode(encodePwd);

        encodeName=base64Encoder(name);
        encodePwd=base64Encoder(pwd);

        name=base64Decoder(encodeName);
        pwd=base64Decoder(encodePwd);

        String token=generateAutoLoginToken(name,pwd);

        System.out.println("token: "+token);
    }
}
